package cn.harry.modular.achievement.controller;

import cn.harry.common.annotation.SysLog;
import cn.harry.common.api.R;
import cn.harry.common.enums.BusinessType;
import cn.harry.component.security.utils.SecurityUtils;
import cn.harry.modular.achievement.AchievementAuditVO;
import cn.harry.modular.achievement.domain.AchievementResearchProjects;
import cn.harry.modular.achievement.enums.NoticeTypeEnums;
import cn.harry.modular.achievement.service.AchievementResearchProjectsService;
import cn.harry.modular.achievement.service.NoticeService;
import cn.harry.modular.system.domain.SysUser;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
* 科研项目表前端控制层
*
* @author harry
* @公众号 Harry技术
*/
@Tag(name = "科研项目表接口")
@RestController
@RequestMapping("/achievement/researchProjects")
@RequiredArgsConstructor
public class AchievementResearchProjectsController  {

    private final AchievementResearchProjectsService achievementResearchProjectsService;
    private final NoticeService noticeService;
    /**
    * 分页查询列表
    */
    @Operation(summary = "分页查询")
    @GetMapping(value = "/page")
        public R<IPage<AchievementResearchProjects>> page(@ParameterObject Page<AchievementResearchProjects> page,@ParameterObject  AchievementResearchProjects entity) {
        return R.success(achievementResearchProjectsService.page(page, Wrappers.lambdaQuery(entity)));
    }

    /**
    * 根据id获取详情
    */
    @Operation(summary = "根据id获取详情")
    @GetMapping(value = "/{id}")
    public R<AchievementResearchProjects> getById(@PathVariable Long id) {
        return R.success(achievementResearchProjectsService.getById(id));
    }

    /**
    * 新增
    */
    @Operation(summary = "新增")
    @SysLog(title = "achievement_research_projects", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Integer> save(@RequestBody AchievementResearchProjects entity) {
        SysUser user = SecurityUtils.getSysUser();
        entity.setUserId(user.getId());
        return achievementResearchProjectsService.save(entity) ? R.success() : R.failed();
    }

    /**
    * 更新
    */
    @Operation(summary = "更新")
    @SysLog(title = "achievement_research_projects", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Integer> update(@RequestBody AchievementResearchProjects entity) {
        return achievementResearchProjectsService.updateById(entity) ? R.success() : R.failed();
    }

    /**
    * 删除
    */
    @Operation(summary = "删除")
    @SysLog(title = "achievement_research_projects", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/{ids}")
    public R<Integer> deleteByIds(@Parameter(description = "多个以英文逗号(,)拼接") @PathVariable Long[] ids) {
        return achievementResearchProjectsService.removeBatchByIds(Arrays.asList(ids)) ? R.success() : R.failed();
    }

    @Operation(summary = "下载")
    @GetMapping(value = "/download/{id}")
    public R<Integer> download(@PathVariable Long id) {
        AchievementResearchProjects entity = achievementResearchProjectsService.getById(id);
        entity.setDownloadCount(entity.getDownloadCount() + 1);
        SysUser user = SecurityUtils.getSysUser();

        String content = StrUtil.format("《{}》被 {} 点击下载", entity.getProjectName(), user.getUsername());
        noticeService.saveInfo(user.getId(), content, NoticeTypeEnums.ACHIEVEMENT_SHARE);
        return achievementResearchProjectsService.updateById(entity) ? R.success() : R.failed();
    }

    @Operation(summary = "审核")
    @SysLog(title = "achievement", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/audit")
    public R<Integer> audit(@RequestBody AchievementAuditVO vo) {
        return achievementResearchProjectsService.audit(vo) ? R.success() : R.failed();
    }
}